package vue;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;


import modele.Equipe;
import modele.Match;

public class PopupNouveauxMatchs_old extends JFrame implements ActionListener {

	private JTable table;
	private JScrollPane scroll;
	private DefaultTableModel model;
	private FenetrePrincipale ihm;
	private JButton valider, annuler;
	private int nbLigneCrees = 10; //A Utiliser si on souhaite ajouter plusieurs lignes ensuites

	public PopupNouveauxMatchs_old(FenetrePrincipale fep) {
		this.ihm = fep;

		setTitle("Nouveaux Matchs");
		setLocation(50, 50);
		setSize(400, 300);

		model = new DefaultTableModel() {

			public boolean isCellEditable(int row, int column) {
				return true; //column != 0 && column != 1 && column != 4;
			}
		};

		table = new JTable(model);

		model.addColumn("Date");
		model.addColumn("Equipe 1");
		model.addColumn("Equipe 2");
		model.addColumn("Lieu");

		table.getColumnModel().getColumn(0).setPreferredWidth(40);
		
		TableColumn sportColumnEq1 = table.getColumnModel().getColumn(1);
		TableColumn sportColumnEq2 = table.getColumnModel().getColumn(2);
		JComboBox comboBox = new JComboBox();
		comboBox.addItem("");
		for(Equipe equipe : ihm.getControleur().getEquipes()) {
			comboBox.addItem(equipe.getNom());
		}
		
		sportColumnEq1.setCellEditor(new DefaultCellEditor(comboBox));
		sportColumnEq2.setCellEditor(new DefaultCellEditor(comboBox));
		
		
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		String dateMatch = "";
		
		Date dateNow = new Date();
		ArrayList<Match> alMatchs = ihm.getControleur().getMatchsAPronostiquer();
		Collections.sort(alMatchs);
		for (int i=0; i<10; i++) {
			model.addRow(new Object[] {"", "", "", "" }); 
		}
		
		scroll = new JScrollPane(table);
		scroll.setPreferredSize(new Dimension(250, 200));

		this.add(scroll);

		JPanel panelBas = new JPanel(new FlowLayout());

		valider = new JButton("Valider");
		valider.addActionListener(this);

		annuler = new JButton("Annuler");
		annuler.addActionListener(this);

		panelBas.add(valider);
		panelBas.add(annuler);

		this.add(panelBas, BorderLayout.SOUTH);

		this.setVisible(true);
	}

	public DefaultTableModel getTableModel() {
		return model;
	}

	public JTable getTable() {
		return table;
	}

	class ClientsTableButtonRenderer extends JButton implements
			TableCellRenderer {
		public ClientsTableButtonRenderer() {
			setOpaque(false);
			this.setText("Infos");
		}

		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			setForeground(Color.black);
			setBackground(UIManager.getColor("Button.background"));
			setText("Infos");
			return this;
		}
	}

	public class ClientsTableRenderer extends DefaultCellEditor {
		private JButton button;
		private String label;
		private boolean clicked;
		private int row, col;
		private JTable table;

		public ClientsTableRenderer(JCheckBox checkBox) {
			super(checkBox);
			button = new JButton("Infos");
			button.setText("Infos");
			button.setOpaque(true);
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					fireEditingStopped();
				}
			});
		}

		public Component getTableCellEditorComponent(JTable table,
				Object value, boolean isSelected, int row, int column) {
			this.table = table;
			this.row = row;
			this.col = column;

			button.setForeground(Color.black);
			button.setBackground(UIManager.getColor("Button.background"));
			label = (value == null) ? "" : value.toString();
			button.setText("Infos");
			clicked = true;
			return button;
		}

		public Object getCellEditorValue() {
			if (clicked) {

				JOptionPane.showMessageDialog(button, "RAJOUTER INFOS MATCH "
						+ table.getSelectedRow());
			}
			clicked = false;
			return new String("Infos");
		}

		public boolean stopCellEditing() {
			clicked = false;
			return super.stopCellEditing();
		}

		protected void fireEditingStopped() {
			super.fireEditingStopped();
		}
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == valider) {
			Date dateMatch;
			String dateMatchString;
			String eq1;
			String eq2;
			String lieu;
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
			boolean saisieCorrect = true;
			for(int i=0; i<nbLigneCrees; i++) {
				dateMatchString = model.getValueAt(i,0).toString();
				eq1 = model.getValueAt(i,1).toString();
				eq2 = model.getValueAt(i,2).toString();
				lieu = model.getValueAt(i,3).toString();
				if(!dateMatchString.equals("") || !eq1.equals("") || !eq2.equals("") || !lieu.equals("")) {
					dateMatch = null;
					try {
						dateMatch = f.parse(model.getValueAt(i,0).toString());
					} catch (ParseException p) { 
						saisieCorrect = false;
						JOptionPane.showMessageDialog(this, "La date est dans un format incorrect. Utiliser aaaa-mm-jj.", "Erreur de date", JOptionPane.ERROR_MESSAGE); 
						break;
					}
					
					Date dateNow = new Date();
					if(dateMatch.before(dateNow)) {
						saisieCorrect = false;
						JOptionPane.showMessageDialog(this, "Une date est dans le passé.", "Erreur de date", JOptionPane.ERROR_MESSAGE); 
						break;
					}
					
					Equipe equipe1 = ihm.getControleur().getEquipeViaNom(eq1);
					Equipe equipe2 = ihm.getControleur().getEquipeViaNom(eq2);
					if(equipe1 == null || equipe2 == null){ 
						saisieCorrect = false;
						JOptionPane.showMessageDialog(this, "Une équipe n'a pas d'adversaire.", "Erreur d'équipe", JOptionPane.ERROR_MESSAGE); 
						break;
					}
					
					if(equipe1.equals(equipe2)) {
						saisieCorrect = false;
						JOptionPane.showMessageDialog(this, "Une équipe ne peut pas s'affronter.", "Erreur d'équipe", JOptionPane.ERROR_MESSAGE); 
						break;
					}
					
					Match match = new Match(equipe1, equipe2, dateMatch, lieu, -1, -1);
					if(ihm.getControleur().isMatchExist(match)) {
						saisieCorrect = false;
						JOptionPane.showMessageDialog(this, "Le match "+equipe1.getNom()+" - "+equipe2.getNom()+" le "+dateMatchString+" existe déjà.", "Double match", JOptionPane.ERROR_MESSAGE); 
						break;
					}
				}
			}
			if(saisieCorrect) {
				for(int i=0; i<nbLigneCrees; i++) {
					dateMatchString = model.getValueAt(i,0).toString();
					eq1 = model.getValueAt(i,1).toString();
					eq2 = model.getValueAt(i,2).toString();
					lieu = model.getValueAt(i,3).toString();
					if(!dateMatchString.equals("") && !eq1.equals("") && !eq2.equals("")) {
						dateMatch = null;
						try {
							dateMatch = f.parse(model.getValueAt(i,0).toString());
						} catch (ParseException p) {}
						
						Equipe equipe1 = ihm.getControleur().getEquipeViaNom(eq1);
						Equipe equipe2 = ihm.getControleur().getEquipeViaNom(eq2);
						
						Match match = new Match(equipe1, equipe2, dateMatch, lieu, -1, -1);
						ihm.getControleur().getMatchsAPronostiquer().add(match);
						equipe1.addMatch(match);
						equipe2.addMatch(match);
					}
				}
				ihm.getPanelPronostique().majContenu();
			}
		} else if(e.getSource() == annuler)
			this.dispose();
	}
}
